import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

import '../../api/DeviceApi.dart';
import '../../commponets/CustomSwitch.dart';
import '../../commponets/ItemView.dart';
import '../../home/AppLocale.dart';
import '../../tools/widgets/my_app_bar.dart';
import '../provider/CustomBottomPicker.dart';
import 'model/Device.dart';

class DeviceIntercomPage extends StatefulWidget {
  const DeviceIntercomPage({
    super.key,
    this.deviceId,
    this.device,
  });

  final int? deviceId;
  final Device? device;

  @override
  State<DeviceIntercomPage> createState() => _DeviceIntercomPageState();
}

class _DeviceIntercomPageState extends State<DeviceIntercomPage> {
  final DeviceApi _deviceApi = DeviceApi();
  late int _deviceId;
  Device? _device;
  late int interCom; // 对讲状态变量



  @override
  void initState() {
    super.initState();

    _deviceId = widget.deviceId!;
    _device = widget.device!;
    interCom = _device?.timeWatermark ?? 0; // 初始化时同步timeWatermark的值
    // _fetchData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: AppLocale.text('d_duijiangshezhi'),
        backgroundColor: Colors.transparent,
        dropdownWidth: 140,
        dropdownBackgroundColor: Colors.white,
        dropdownBorderRadius: 20,
      ),
      body: Container(
        child: Column(
          children: [
            const SizedBox(height: 20),
            ItemView(
              width: 400,
              height: 50,
              leftPadding: 10,
              rightPadding: 20,
              leftWidget: Row(
                children: [
                  const SizedBox(width: 10),
                  Text(
                    AppLocale.text('d_shijianshuiin'),
                    style: TextStyle(fontSize: 15.0, color: Colors.black),
                  ),
                ],
              ),
              rightWidget: Row(
                children: [
                  CustomSwitch(
                    value: _device?.timeWatermark ?? 0,
                    size: 25.0,
                    onChanged: (newValue) {
                      final toUpdate = {
                        "deviceId": _device?.deviceId,
                        "timeWaterMark": newValue,
                      };
                      _deviceApi.timeWaterMark(toUpdate);
                      setState(() {
                        _device?.timeWatermark = newValue;
                        interCom = newValue; // 同步更新interCom的值
                      });
                    },
                    activeBackgroundColor: Colors.green,
                    inactiveBackgroundColor: Colors.grey[300]!,
                  ),
                ],
              ),
              borderRadius: 8,
              onTap: () {},
            ),
            ItemView(
              width: 400,
              height: 50,
              leftPadding: 10,
              rightPadding: 20,
              leftWidget: Row(
                children: [
                  const SizedBox(width: 10),
                  Text(
                    AppLocale.text('d_duijiangfangshi'),
                    style: TextStyle(fontSize: 15.0, color: Colors.black),
                  ),
                ],
              ),
              rightWidget: Row(
                children: [
                  Row(
                    children: [
                      Text(
                        _device?.intercomMode == 0
                            ? AppLocale.text('d_danxiangduijiang')
                            : _device?.intercomMode == 1
                            ? AppLocale.text('d_shaugnxiangduijiang')
                            : AppLocale.text('d_qitaduijiang'),
                        style: TextStyle(
                            fontSize: 14.0,
                            color: Colors.black54
                        ),
                      ),
                      const SizedBox(width: 5,),
                      const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey,
                        size: 14,
                      ),
                    ],
                  )
                ],
              ),
              borderRadius: 8,
              onTap: () {

                // dynamic ioSelect = 1;
                dynamic ioSelect = _device?.intercomMode;

                // 选项列表（label 是显示文案，value 是实际值）
                List<Map<String, dynamic>> ioList = [
                  {'label': AppLocale.text('d_danxiangduijiang'), 'value': 0},
                  {'label': AppLocale.text('d_shaugnxiangduijiang'), 'value': 1},
                ];


                showModalBottomSheet(
                  context: context,
                  backgroundColor: Colors.transparent,
                  builder: (BuildContext dialogContext) {
                    return CustomBottomPicker(
                      title: AppLocale.text('d_duijiangfangshi'),
                      height: 300,
                      list: ioList,
                      selectedColor: Colors.blue,
                      backgroundColor: Colors.white,
                      initialValue: ioSelect,
                      //圆圈样式
                      selectedShapeType: SelectedShapeType.hollowCircle,
                      onSelect: (newValue) {

                        setState(() {
                          _device?.intercomMode = newValue;
                        });

                        final toUpdate = {
                          "deviceId": _device?.deviceId,
                          "interComMode": newValue,
                        };
                        _deviceApi.interComMode(toUpdate);

                      },
                    );
                  },
                );
              },
            ),



          ],
        ),
      ),
    );
  }
}
